package test.h0817;

import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        Pattern p = Pattern.compile("A=\\{([\\d,]+)\\},B=\\{([\\d,]+)},R=(\\d+)");
        Matcher matcher = p.matcher(s);
        if (matcher.find()) {
            int[] A = Arrays.stream(matcher.group(1).split(",")).mapToInt(Integer::parseInt).toArray();
            int[] B = Arrays.stream(matcher.group(2).split(",")).mapToInt(Integer::parseInt).toArray();
            int R = Integer.parseInt(matcher.group(3));
            String res = getResult(A, B, R);
            System.out.println(res);
        }
    }

    private static String getResult(int[] a, int[] b, int r) {
        //AB数组已经有序
        int l1 = a.length;
        int l2 = b.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < l1; i++) {
            int cnt = 0;
            for (int j = 0; j < l2; j++) {
                if (a[i] <= b[j]) {
                    if (b[j] - a[i] <= r || cnt == 0) {
                        sb.append("(").append(a[i]).append(",").append(b[j]).append(")");
                        cnt++;//控制找到最近的B[j]
                    }else {
                        break;
                    }
                }
            }
        }
        return sb.toString();
    }
}
